#ifndef PWM_H
#define PWM_H

/**
 * @defgroup nxbot_pwm PWM Library
 * @ingroup nxbot_low_level
 * @code #include <pwm.h> @endcode
 * @brief PWM library for applying different velocities to the motors in NXBOT. 
 *
 * This module uses timer0 of the ATmega1281 microcontroller for generating the desired PWM signals. Timer0 is configured in <i>Free Running mode</i>,
 * and it is constantly compared with two registers (OC0 and OC1), in order to toggle the logical state of two pins, where the PWM signals to the
 * motors are connected. Another two signals are used in order to set the desired direction of the motor. For FORWARD direction use <b>positive</b>
 * values, for BACKWARD direction use <b>negative</b> values.
 * This library does not need to have the interrupts in order to function correctly, it is not an interrupt-driven module.
 * 
 */
 
/**@{*/

#include <avr/io.h>
#include "nxbot.h"

/**
 * @brief 16MHz PWM frequency. 
 * @see pwm_setFreq
 */
#define PWM_FREQUENCY_16M 1
/**
 * @brief 2MHz PWM frequency. 
 * @see pwm_setFreq
 */
#define PWM_FREQUENCY_2M 2
/**
 * @brief 250kHz PWM frequency. 
 * @see pwm_setFreq
 */
#define PWM_FREQUENCY_250K 3
/**
 * @brief 62kHz PWM frequency. 
 * @see pwm_setFreq
 */
#define PWM_FREQUENCY_62K 4
/**
 * @brief 15kHz PWM frequency. 
 * @see pwm_setFreq
 */
#define PWM_FREQUENCY_15K 5


/**
 * @brief Max. value applicable to PWM hardware.
 */
#define PWM_MAX 127

/**
 * @brief Hardware initialization of the %pwm module.
 *
 * Configures the PWM in order to control motor velocities. As this is not an interrupt-driven module, there is not need to call sei() function.
 *
 * @param freq Frequency of the PWM signal. It should be one of the PWM_FREQUENCY_ constants defined above, e.g. PWM_FREQUENCY_15K
 */
void pwm_init(unsigned char freq);

/**
 * @brief Sets the frequency of the PWM signal.
 *
 * @param freq Frequency of the PWM signal. It should be one of the PWM_FREQUENCY_ constants configured above, e.g. PWM_FREQUENCY_15K
 */
void pwm_setFreq(unsigned char freq);

/**
 * @brief Gets the preconfigured frequency of the PWM signal.
 *
 * @return Frequency of the PWM signal. It is one of the PWM_FREQUENCY_ constants defined above, e.g. PWM_FREQUENCY_15K
 */
unsigned char pwm_getFreq(void);

/**
 * @brief Write desired PWM value for right motor.
 *
 * @param right Desired PWM value for right motor. The max value applicable is PWM_MAX, which is 127. You can also set negative values for backward
 * direction of the motor.
 *        
 */
void pwm_writeRight(signed char right);

/**
 * @brief Write desired PWM value for left motor.
 *
 * @param left Desired PWM value for left motor. The max value applicable is PWM_MAX, which is 127. You can also set negative values for backward
 * direction of the motor.
 *        
 */
void pwm_writeLeft(signed char left);

/**
 * @brief Write desired PWM value for both left and right %motors.
 *
 * @param left Desired PWM value for left motor. Same as in pwm_writeLeft().
 * @param right Desired PWM value for right motor. Same as in pwm_writeRight(). 
 *        
 */
void pwm_write(signed char left, signed char right);

/**@}*/
#endif
